package com.jdls.day19.work;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 嘉德罗斯
 * @date 2021/8/10
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        ThreadPoolExecutor te = new ThreadPoolExecutor(3,//正式工,
                10,//临时工
                60,//时间
                TimeUnit.SECONDS,//时间单位
                new ArrayBlockingQueue(5),//阻塞队列
                Executors.defaultThreadFactory(),//创建线程方式
                new ThreadPoolExecutor.AbortPolicy()//拒绝策略
                );
        ServerSocket ss = new ServerSocket(20002);
        while (true) {
            Socket a = ss.accept();
            te.submit(new Thread(()->{
                InputStream is = null;
                try {
                     is = a.getInputStream();
                    int b ;
                    byte[] by = new byte[1024];
                    while ((b = is.read(by))!=-1){
                        System.out.println(new String(by,0,b));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }));
            //a.close();
        }
        //ss.close();
    }
}
